<?php

/**
 * 历史数据的处理,通过调用bll方法，写入数据
*/
apf_require_class('Oa_Job_Attendance_Abstract');
apf_require_class("Tools_Tools");

apf_require_class('Const_LeaveType');
apf_require_class('Bll_Attendance_DailyReport');
apf_require_class('Bll_Attendance_DailyLeave');
apf_require_class('Bll_Attendance_AccessDayRecord');

class Oa_Job_Attendance_TestLeave extends Oa_Job_Attendance_Abstract {
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    public function run($params){
        $daily_bll = new Bll_Attendance_DailyReport();
        $la_bll = new Bll_Attendance_DailyLeave();

        $this->_daily_bll =$daily_bll ;
        $this->_la_bll =$la_bll ;
        $type = $params['type'];
        if($type=='check'){
            $st = $la_bll->get_report_data($params['uid'],$params['start'],$params['end']);
            print_r($st);exit;
        }elseif($type=='check2'){
            $leave_data=$daily_bll->get_leave_record_by_id($params['uid']);
            $daily_bll->update_common_leave($leave_data,100);
        }elseif($type=='check3'){
            $sql_gc = "SELECT * FROM leave_record WHERE id=".$params['uid'];
            $datalist=$this->get_rows($sql_gc,array(),false,$this->writedb);
            foreach ($datalist as $leave_data) {
                $leave_data['start_time'] = strtotime($leave_data['start_time']);
                $leave_data['end_time'] = strtotime($leave_data['end_time']);
                //print_r($leave_data);
                echo $leave_data['type'];
                $leave_data['type'] = $daily_bll->format_leave_type($leave_data['type']);
                $leave_data['status']=$daily_bll->format_leave_status($leave_data['status']);
                $leave_data['daylist']=$daily_bll->format_leave_days($leave_data['start_time'],$leave_data['end_time'],$leave_data['type']);
                $type2=$leave_data['type'];
                var_dump($type2);
                var_dump(Const_LeaveType::LEAVE_TYPE_GONGCHU);

                switch ($type2){
                    case Const_LeaveType::LEAVE_TYPE_CHIDAO:
                        $daily_bll->update_come_late($leave_data,100);
                        break;
                    case Const_LeaveType::LEAVE_TYPE_ZAOTUI:
                        $daily_bll->update_left_early($leave_data,100);
                        break;
                    case Const_LeaveType::LEAVE_TYPE_GONGCHU:
                        $daily_bll->update_business_travel($leave_data,100);
                        break;
                    case Const_LeaveType::LEAVE_TYPE_CHUCHA:
                        $daily_bll->update_business_trip($leave_data, 100);
                        break;
                    default:
                        $daily_bll->update_common_leave($leave_data,100);
                        break;
                }
            }
        }elseif($type=='check4'){
            $sql_gcc=" select a.*,b.user_id,b.name from leave_record as a left join user_info as b ON a.user_id=b.user_id where a.status IN('已批准','未审核') and a.apply_time>='2012-11-29 12:23:00' and a.apply_time<='2012-11-29 23:00:00' order by id desc";
            $datalist = $this->get_rows($sql_gcc,array(),false,$this->writedb);
            foreach($datalist as $val){
                $str="{$val['id']}\t{$val['user_id']}\t{$val['name']}\t{$val['apply_time']}\t{$val['start_time']}==={$val['end_time']}\t{$val['type']}\t{$val['status']}\n";
                file_put_contents('/tmp/e1129.txt',$str,FILE_APPEND);
            }
        }elseif($type=='check5'){
            $start = 0;
            while (true) {
                $sql_gc = "SELECT * FROM leave_record WHERE id>$start AND apply_time>='2012-11-26 00:00:00' order by id asc limit 10";
                $datalist=$this->get_rows($sql_gc,array(),false,$this->writedb);
                if(empty($datalist)) {
                    break;
                }
                foreach ($datalist as $val) {
                    $leave_data=$daily_bll->get_leave_record_by_id($val['id']);
                    switch ($leave_data['type']){
                        case Const_LeaveType::LEAVE_TYPE_CHIDAO:
                            //$daily_bll->update_come_late($leave_data,100);
                            break;
                        case Const_LeaveType::LEAVE_TYPE_ZAOTUI:
                            //$daily_bll->update_left_early($leave_data,100);
                            break;
                        case Const_LeaveType::LEAVE_TYPE_GONGCHU:
                            $this->update_business_travel($leave_data);
                            break;
                        case Const_LeaveType::LEAVE_TYPE_CHUCHA:
                            $this->update_business_trip($leave_data);
                            break;
                        default:
                            $this->update_common_leave($leave_data);
                            break;
                    }
                }
                $num = count($datalist);
                $start = $datalist[$num-1]['id'];
            }
        }elseif($type=='check6'){
            $sle = array(
                    157=>723,
                    155=>721,
                    154=>708,
                    151=>705,
                    150=>703,
                    148=>711,
                    145=>693,
                    144=>692,
                    138=>686,
                    137=>688,
                    136=>685,
                    133=>736,
                    130=>628,
                    129=>627,
                    127=>700,
                    125=>670,
                    124=>669,
                    122=>662,
                    121=>663,
                    120=>659,
                    117=>729,
                    113=>666,
                    111=>717,
                    110=>713,
                    107=>574,
                    106=>674,
                    105=>673,
                    102=>624,
                    101=>623,
                    100=>644,
                    99=>643,
                    98=>642,
                    94=>636,
                    93=>635,
                    90=>698,
                    86=>738,
                    85=>737,
                    81=>618,
                    80=>614,
                    75=>557,
                    74=>555,
                    73=>556,
                    70=>680,
                    69=>678,
                    65=>610,
                    64=>607,
                    63=>602,
                    58=>595,
                    57=>594,
                    56=>592,
                    52=>651,
                    51=>653,
                    50=>650,
                    46=>583,
                    45=>584,
                    44=>580,
                    39=>570,
                    38=>569,
                    37=>567,
                    34=>731,
                    30=>548,
                    29=>546,
                    28=>547,
                    27=>521,
                    25=>520,
                    24=>523,
                    23=>545,
                    21=>534,
                    20=>533,
                    19=>542,
                    18=>506,
                    17=>536,
                    16=>539,
                    15=>538,
                    14=>537,
                    13=>544,
                    12=>529,
                    11=>530,
                    10=>528,
                    9=>526,
                    8=>525,
                    7=>516,
                    4=>514
                    );
            foreach($sle as $new=>$val){
                $upsql = "update trip_manager set department_id={$new} where department_id={$new}";
                $this->execute( $upsql,array(), $this->writedb);
            }
        }
    }

    private function update_business_travel($leave_data){
        $start = date('Ymd',$leave_data['start_time']);
        $today = date('Ymd');
        $end =  date('Ymd',$leave_data['end_time']);
        if($end <=$today){
            $this->log('close...');
        }
        if($start > $today){
            $this->insert_record_kq_status($leave_data['user_id'],$start);
        }
    }

    private function update_business_trip($leave_data){
        $daylist = $leave_data['daylist'];
        foreach($daylist as $day=>$val){
            if($day>$today){#如果是今天之后的数据需要写入user_day_status
                $this->insert_record_kq_status($leave_data['user_id'],$day);
            }
            $up = array(
                    'am_type'=>$leave_data['type'],
                    'am_leave_status'=>$leave_data['status'],
                    'pm_type'=>$leave_data['type'],
                    'pm_leave_status'=>$leave_data['status'],
                    'days'=>$val['am']+$val['pm']
            );
            if($val['apm']==1){
                unset($up['pm_type']);
                unset($up['pm_leave_status']);
            }elseif($val['apm']==2){
                unset($up['am_type']);
                unset($up['am_leave_status']);
            }
            $this->_la_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
        }
    }
    private function update_common_leave($leave_data){
        $daylist = $leave_data['daylist'];
        $this->insert_record_kq_status_multi($daylist,$leave_data['user_id']);
        $result = $this->_daily_bll->split_leave_days($leave_data);
        if(!empty($result['curr'])){
            foreach($result['curr'] as $day=> $val){
                $up = array(
                        'am_type'=>$leave_data['type'],
                        'am_leave_status'=>$leave_data['status'],
                        'pm_type'=>$leave_data['type'],
                        'pm_leave_status'=>$leave_data['status'],
                        'days'=>$val['am']+$val['pm']
                );
                if($val['apm']==1){
                    unset($up['pm_type']);
                    unset($up['pm_leave_status']);
                }elseif($val['apm']==2){
                    unset($up['am_type']);
                    unset($up['am_leave_status']);
                }
                if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_QUEQIN){//如果类型是缺勤,强制不存到kq_status表中
                    //$up=array('days'=>$val['am']+$val['pm']);
                }
                $this->_la_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
            }
        }
        if(!empty($result['prev'])){
           //上月未清的数据,不更新状态,前台显示的通过取到上月未清的数据进行展示
        }
        if(!empty($result['next'])){
            foreach($result['next'] as $day=> $val){
                $up = array(
                        'am_type'=>$leave_data['type'],
                        'am_leave_status'=>$leave_data['status'],
                        'pm_type'=>$leave_data['type'],
                        'pm_leave_status'=>$leave_data['status'],
                        'days'=>$val['am']+$val['pm']
                );
                if($val['apm']==1){
                    unset($up['pm_type']);
                    unset($up['pm_leave_status']);
                }elseif($val['apm']==2){
                    unset($up['am_type']);
                    unset($up['am_leave_status']);
                }
                if($leave_data['type']==Const_LeaveType::LEAVE_TYPE_QUEQIN){//如果类型是缺勤,强制不存到kq_status表中
                    //$up=array('days'=>$val['am']+$val['pm']);
                }
                $this->_la_bll->update_kq_status($up, array('user_id'=>$leave_data['user_id'],'dateline'=>$day));
            }
        }
    }
    private function insert_record_kq_status($uid,$dateline){
        $sql = "SELECT * FROM user_day_status where user_id={$uid} and dateline={$dateline}";
        $data = $this->get_db_row($sql,$this->writedb,array());
        if(empty($data['user_id'])){
            $this->_la_bll->insert_record_kq_status($uid,$dateline);
        }
    }
    private function insert_record_kq_status_multi($daylist,$uid){

        $no_daylist = array();
        foreach($daylist as $day=>$val){
            $sql = "SELECT * FROM user_day_status where user_id={$uid} and dateline={$day}";
            $data = $this->get_db_row($sql,$this->writedb,array());
            if(empty($data['user_id'])){
                $no_daylist[$day] = $val;
            }
        }
        if(!empty($no_daylist)){
            $this->_la_bll->insert_record_kq_status_multi($no_daylist,$uid);
        }
    }
}